package com.haofengsoft.scce.ui.consultation;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.Toast;

import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshBase;
import com.haofengsoft.platform.companent.pulltorefresh.PullToRefreshListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.OptionalStampAdapter;
import com.haofengsoft.scce.adapter.information.OptionalStampModel;
import com.haofengsoft.scce.common.client.VolleyBean;
import com.haofengsoft.scce.common.client.VolleyInterface;
import com.haofengsoft.scce.controller.consultation.OptionalStampController;
import com.haofengsoft.scce.ui.fragment.BaseFragment;

public class OptionalStampFragment extends BaseFragment {

	private PullToRefreshListView optionalStampPtrl;
	private OptionalStampAdapter optionalStampAdapter = null;

	private int pageIndex = 0;
	private Context context;

	public OptionalStampFragment() {
		// 添加访问后台需要的参数
	}

	public static OptionalStampFragment newInstance(String name) {
		Bundle bundle = new Bundle();
		bundle.putString("name", name);
		OptionalStampFragment fragment = new OptionalStampFragment();
		fragment.setArguments(bundle);
		return fragment;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.consultation_optional_stamp, container, false);
		view.setOnTouchListener(new DontSpillOnTouchListener());
		context = getActivity();
		view.setOnTouchListener(new DontSpillOnTouchListener());
		optionalStampPtrl = (PullToRefreshListView) view.findViewById(R.id.consultation_optional_stamp_ptrl);
		optionalStampPtrl.setMode(PullToRefreshBase.Mode.DISABLED);

		ImageView nodataIv = (ImageView) view.findViewById(R.id.optional_stamp_no_data_iv);
		nodataIv.setOnClickListener(new DataLoadOnClickListener());
		optionalStampPtrl.setEmptyView(nodataIv);
		optionalStampPtrl.setOnFocusChangeListener(null);
		optionalStampPtrl.setOnHierarchyChangeListener(null);
		optionalStampPtrl.setOnRefreshListener(new LoadPullData());
		initVolleyData();

		return view;
	}

	class LoadPullData implements PullToRefreshBase.OnRefreshListener2 {
		// 下拉(手势下拉)
		@Override
		public void onPullDownToRefresh(PullToRefreshBase refreshView) {
			pageIndex = 0;
			optionalStampAdapter = null;
			initVolleyData();
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase refreshView) {
			initVolleyData();
		}
	}

	class DataLoadOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {
			initVolleyData();
		}
	}

	@Override
	protected void lazyLoad() {

	}

	private enum OptionalStamp {
		ONE("牛股汇：中期业绩成试金石三金发力周涨6...", "（腾讯证券讯）本周A股灾后重建第二周，沪指一度站上4100点，最低下探至3...", "07-25 07:05"),

		TWO("涨停板预测：天广消防等17股有望冲涨停", "腾讯证券每日晚间根据上市公司公告、财务报表以及市场热点等多方面内容，甄选出...", "07-24 21:06"),

		THREE("揭秘涨停板：电改顶层设计呼之欲出 3股涨停", "（腾讯证券华烨发自北京）周五两市双双高开，早盘石油、煤炭、有色接力拉升股指...", "07-24 15：31"),

		FOUR("涨停板预测：三湘股份等24股有望冲涨停", "本文根据上市公司公告、财务报表以及市场热点等多方面内容，甄选出部分有望冲刺...", "07-23 21：11"),

		FIVE("揭秘涨停板：文科园林等129只次新股掀狂潮", "（腾讯证券华烨发自北京）周五沪指涨逾2%突破4100点，实现六连阳，板块全...", "07-23 16：09");

		String title;
		String overView;
		String time;

		OptionalStamp(String title, String overView, String time) {
			this.title = title;
			this.overView = overView;
			this.time = time;
		}

		public String getTitle() {
			return title;
		}

		public String getOverView() {
			return overView;
		}

		public String getTime() {
			return time;
		}

	}

	private void refreshList(List<OptionalStampModel> modelList) {
		if (null != modelList) {
			if (null == optionalStampAdapter) {
				optionalStampAdapter = new OptionalStampAdapter(context, modelList);
				optionalStampPtrl.setAdapter(optionalStampAdapter);
			} else {
				optionalStampAdapter.addOptionalStampList(modelList);
			}
			pageIndex = pageIndex + 1;
			optionalStampAdapter.notifyDataSetChanged();
		} else {
			Toast.makeText(getActivity(), "没有信息了", Toast.LENGTH_SHORT).show();
		}
		optionalStampPtrl.onRefreshComplete();
	}

	private void initVolleyData() {

		List<OptionalStampModel> modelList = new ArrayList<OptionalStampModel>();
		for (OptionalStamp optionalStamp : OptionalStamp.values()) {
			OptionalStampModel optionalStampModel = new OptionalStampModel();
			optionalStampModel.setId("3");
			optionalStampModel.setDetail(optionalStamp.getOverView());
			optionalStampModel.setSoure("腾讯证券");
			optionalStampModel.setTime(optionalStamp.getTime());
			optionalStampModel.setTitle(optionalStamp.getTitle());
			modelList.add(optionalStampModel);
		}

		refreshList(modelList);

		// Map<String, Object> mapParam = new HashMap<String, Object>();
		// mapParam.put("PageIndex", pageIndex);
		// mapParam.put("PageSize", 10);
		// mapParam.put("TypeID", 1);
		//
		// VolleyManage.getInstance().methodPost(getActivity(), null,
		// ConstantConfig.TYPE_NEWS_URL, mapParam,
		// new OptionalStampVolleyReal());

	}

	class OptionalStampVolleyReal implements VolleyInterface {
		@Override
		public void gainData(VolleyBean bean) {
			if (bean.isSuccess()) {
				List<OptionalStampModel> modelList = OptionalStampController.getInstance(getActivity())
						.handleopOptionalStampModelList(bean.getContent());
				refreshList(modelList);
			} else {
				Toast.makeText(getActivity(), "网络连接错误", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
